/*
 * Copyright (C) 2010-2011 Comcast Cable.  All Rights Reserved.
 */
package com.hercules.android.receiver.painting.resources;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

import com.hercules.android.receiver.LogUtil;
import com.hercules.android.receiver.model.commands.XRECommand;
import com.hercules.android.receiver.model.commands.XRESetCommand;
import com.hercules.android.receiver.model.objects.XREImage;
import com.hercules.android.receiver.model.objects.XREResourceOptions;
import com.hercules.android.receiver.painting.ImageData;
import com.hercules.android.receiver.painting.PaintersUtil;
import com.hercules.android.receiver.painting.XREObjectPainterException;

/**
 * Painter for {@link XREImage} resource. First it will load image from URL and then paint it on screen using XREView's
 * {@link XREResourceOptions}.
 * <p>
 * AndroidAssembly1 this class was added in Android Assembly 1
 * <p>
 * <b>Thread safety:</b> Not thread safe, because base class is not thread safe
 *
 * @author 7realm
 * @version 1.0
 */
public class XREImagePainter extends BaseXREResourcePainter {
    /**
     * Constructor for class {@link XREImagePainter}.
     *
     * @param xreResource the {@link XREImage}
     */
    public XREImagePainter(XREImage xreResource) {
        super(xreResource);
    }

    /**
     * Handle {@link XRECommand}.
     *
     * @param command the command to handle
     * @throws XREObjectPainterException if error occurs while handling command
     */
    public void handleCommand(XRECommand command) throws XREObjectPainterException {
        super.handleCommand(command);
        
        if (command instanceof XRESetCommand) {
            XRESetCommand setCommand = (XRESetCommand) command;
            
            if (setCommand.getProps().containsKey("url")) {
            	init();
            }
        }
    }
    
    /**
     * Performs initialize actions.
     *
     * @throws IllegalStateException if required fields are not set
     * @throws XREObjectPainterException if failed to load image
     */
    @Override
    public void init() throws XREObjectPainterException {
        // do nothing
    }

    /**
     * Performs clean up.
     *
     * @throws IllegalStateException if required fields are not set
     * @throws XREObjectPainterException if error occurs while clean up
     */
    @Override
    public void finish() throws XREObjectPainterException {
        // nothing
    }

    /**
     * Draw resource on surface.
     *
     * @param canvas the canvas to be used
     */
    @Override
    public void onDraw(Canvas canvas) {
        try {
            // get the image
            Bitmap image = PaintersUtil.getBitmap(getImageUrl());
            if (image != null) {
                // calculate the region to draw according to resource parameters
                Rect rect = PaintersUtil.calculateStretchAndAling(getXREView(), image.getWidth(), image.getHeight());

                // AndroidAssembly2 added clipping
                // clip the image
                canvas.clipRect(0, 0, getXREView().getWidth(), getXREView().getHeight());

                // draw the image
                canvas.drawBitmap(image, null, rect, null);
            }
        } catch (XREObjectPainterException e) {
            LogUtil.error(getLogger(), this, "onDraw", e);
        }
    }

    /**
     * Get resource's measured width.
     *
     * @return measured width of resource
     */
    @Override
    public int getMeasuredWidth() {
        try {
            // get the image
            Bitmap image = PaintersUtil.getBitmap(getImageUrl());
            if (image != null) {
                // calculate the region to draw according to resource parameters
                Rect rect = PaintersUtil.calculateStretchAndAling(getXREView(), image.getWidth(), image.getHeight());

                return rect.width();
            }
        } catch (XREObjectPainterException e) {
            LogUtil.error(getLogger(), this, "getMeasuredWidth", e);
        }

        return super.getMeasuredWidth();
    }

    /**
     * Get resource's measured height.
     *
     * @return measured height of resource
     */
    @Override
    public int getMeasuredHeight() {
        try {
            // get the image
            Bitmap image = PaintersUtil.getBitmap(getImageUrl());
            if (image != null) {
                // calculate the region to draw according to resource parameters
                Rect rect = PaintersUtil.calculateStretchAndAling(getXREView(), image.getWidth(), image.getHeight());

                return rect.height();
            }
        } catch (XREObjectPainterException e) {
            LogUtil.error(getLogger(), this, "getMeasuredHeight", e);
        }

        return super.getMeasuredHeight();
    }

    /**
     * Gets {@link XREImage} url.
     *
     * @return the image url
     */
    protected String getImageUrl() {
        return getXREImage().getUrl();
    }

    /**
     * Gets casted resource.
     *
     * @return the {@link XREImage}
     */
    protected XREImage getXREImage() {
        return (XREImage) getXREResource();
    }
}
